/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */
package org.eclipse.mosaic.lib.objects.trafficlight;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Class represents the parameters of traffic lights
 */
public class TrafficLightParam  implements Serializable {

    private static final long serialVersionUID = 1L;

    private final String trafficLightGroupId;

    private final String programId;

    private final Map<String, List<TrafficLightLink>> landmark2Link = new HashMap<>();

    private final Map<TrafficLightLink, String> link2Landmark = new HashMap<>();

    public TrafficLightParam(String trafficLightGroupId, String programId, Map<String, String> params) {
        this.trafficLightGroupId = trafficLightGroupId;
        this.programId = programId;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            int linkIndex = Integer.parseInt(entry.getKey().split(":")[1]);
            String landmarkId = entry.getValue();

            if (!this.landmark2Link.containsKey(landmarkId)) {
                landmark2Link.put(landmarkId, new ArrayList<>());
            }
            TrafficLightLink trafficLightLink = new TrafficLightLink(trafficLightGroupId, linkIndex);
            this.landmark2Link.get(landmarkId).add(trafficLightLink);
            this.link2Landmark.put(trafficLightLink, landmarkId);
        }
    }

    public String getTrafficLightGroupId() {
        return trafficLightGroupId;
    }

    public String getProgramId() {
        return programId;
    }

    public Map<String, List<TrafficLightLink>> getLandmark2Link() {
        return landmark2Link;
    }

    public Map<TrafficLightLink, String> getLink2Landmark() {
        return link2Landmark;
    }

    public static class TrafficLightLink {
        private final String trafficLightGroupId;
        private final int linkIndex;

        public TrafficLightLink(String trafficLightGroupId, int linkIndex) {
            this.trafficLightGroupId = trafficLightGroupId;
            this.linkIndex = linkIndex;
        }

        public String getTrafficLightGroupId() {
            return trafficLightGroupId;
        }

        public int getLinkIndex() {
            return linkIndex;
        }
    }
}
